#Replication package for Kim, DellaPosta, and Essig
#Code for analyzing experimental data

rm(list=ls())

##T-Tests Reported in Text#######

#Respondents' Own Opinions

dem <- read.csv("study_democrats1.csv")
dem <- subset(dem, !(is.na(dem$gender))) #remove respondents who didn't finish survey or don't typically support Democrats
dem <- subset(dem, dem$partyaff>2 & dem$partyaff<6)

rep <- read.csv("study_republicans1.csv")
rep <- subset(rep, !(is.na(rep$gender)))
rep <- subset(rep, rep$partyaff<4) #remove the democrat and "other" responses

comparison_value <- 4 #midpoint response on opinion questions

#Democrats, Gun Control
dem$guncontrol_own <- dem$guncontrol_own_c2
dem$guncontrol_own[dem$politiciansurvey==3] <- dem$guncontrol_own_c3[dem$politiciansurvey==3]
dem$guncontrol_own[dem$politiciansurvey==4] <- dem$guncontrol_own_c4[dem$politiciansurvey==4]

t.test(dem$guncontrol_own, mu = comparison_value)

#Democrats, Climate Change
dem$climatechange_own <- dem$climatechange_own_c2
dem$climatechange_own[dem$politiciansurvey==3] <- dem$climatechange_own_c3[dem$politiciansurvey==3]
dem$climatechange_own[dem$politiciansurvey==4] <- dem$climatechange_own_c4[dem$politiciansurvey==4]

t.test(dem$climatechange_own, mu = comparison_value)

#Democrats, Health Care
dem$medicare_own <- dem$healthcare_own_c2
dem$medicare_own[dem$politiciansurvey==3] <- dem$medicare_own_c3[dem$politiciansurvey==3]
dem$medicare_own[dem$politiciansurvey==4] <- dem$medicare_own_c4[dem$politiciansurvey==4]

t.test(dem$medicare_own, mu = comparison_value)

#Democrats, Police
dem$police_own <- dem$police_own_c2
dem$police_own[dem$politiciansurvey==3] <- dem$police_own_c3[dem$politiciansurvey==3]
dem$police_own[dem$politiciansurvey==4] <- dem$police_own_c4[dem$politiciansurvey==4]

t.test(dem$police_own, mu = comparison_value)

#Republicans, Climate Change
rep$climatechange_own <- rep$climatechange_own_c2
rep$climatechange_own[rep$politiciansurvey==3] <- rep$climatechange_own_c3[rep$politiciansurvey==3]
rep$climatechange_own[rep$politiciansurvey==4] <- rep$climatechange_own_c4[rep$politiciansurvey==4]

t.test(rep$climatechange_own, mu = comparison_value)

#Republicans, Police
rep$police_own <- rep$police_own_c2
rep$police_own[rep$politiciansurvey==3] <- rep$police_own_c3[rep$politiciansurvey==3]
rep$police_own[rep$politiciansurvey==4] <- rep$police_own_c4[rep$politiciansurvey==4]

t.test(rep$police_own, mu = comparison_value)

#Republicans, Right to Refuse Service
rep$righttorefuse_own <- rep$righttorefuse_own_c2
rep$righttorefuse_own[rep$politiciansurvey==3] <- rep$righttorefuse_own_c3[rep$politiciansurvey==3]
rep$righttorefuse_own[rep$politiciansurvey==4] <- rep$righttorefuse_own_c4[rep$politiciansurvey==4]

t.test(rep$righttorefuse_own, mu = comparison_value)

#Republicans, Gun Control
rep$guncontrol_own <- rep$guncontrol_own_c2
rep$guncontrol_own[rep$politiciansurvey==3] <- rep$guncontrol_own_c3[rep$politiciansurvey==3]
rep$guncontrol_own[rep$politiciansurvey==4] <- rep$guncontrol_own_c4[rep$politiciansurvey==4]

t.test(rep$guncontrol_own, mu = comparison_value)

#For t-tests comparing respondents' inferences on the two politicians, see section below for reproducing Figure 4


##REPRODUCING FIGURE 4#######
rm(list=ls())

library(broom)
library(dplyr)
library(ggplot2)
library(cowplot)

#DEMOCRATS####
data_d <- read.csv("study_democrats1.csv")
data_d <- subset(data_d, !(is.na(data_d$gender)))
data_d <- subset(data_d, data_d$partyaff>2 & data_d$partyaff<6)

#combine outcome variables
data_d$PoliticianAfuture <- data_d$PoliticianAfuture_c1
data_d$PoliticianAfuture[data_d$politiciansurvey==3] <- data_d$PoliticianAfuture_c3[data_d$politiciansurvey==3]
data_d$PoliticianAfuture[data_d$politiciansurvey==4] <- data_d$PoliticianAfuture_c4[data_d$politiciansurvey==4]

data_d$PoliticianBfuture <- data_d$PoliticianBfuture_c1
data_d$PoliticianBfuture[data_d$politiciansurvey==3] <- data_d$PoliticianBfuture_c3[data_d$politiciansurvey==3]
data_d$PoliticianBfuture[data_d$politiciansurvey==4] <- data_d$PoliticianBfuture_c4[data_d$politiciansurvey==4]

data_d$PoliticianAhealth <- data_d$PoliticianAhealth_c1
data_d$PoliticianAhealth[data_d$politiciansurvey==3] <- data_d$PoliticianAhealth_c3[data_d$politiciansurvey==3]
data_d$PoliticianAhealth[data_d$politiciansurvey==4] <- data_d$PoliticianAhealth_c4[data_d$politiciansurvey==4]

data_d$PoliticianBhealth <- data_d$PoliticianBhealth_c1
data_d$PoliticianBhealth[data_d$politiciansurvey==3] <- data_d$PoliticianBhealth_c3[data_d$politiciansurvey==3]
data_d$PoliticianBhealth[data_d$politiciansurvey==4] <- data_d$PoliticianBhealth_c4[data_d$politiciansurvey==4]

data_d$VoteA <- data_d$VoteA_c1
data_d$VoteA[data_d$politiciansurvey==3] <- data_d$VoteA_c3[data_d$politiciansurvey==3]
data_d$VoteA[data_d$politiciansurvey==4] <- data_d$VoteA_c4[data_d$politiciansurvey==4]

data_d$VoteB <- data_d$VoteB_c1
data_d$VoteB[data_d$politiciansurvey==3] <- data_d$VoteB_c3[data_d$politiciansurvey==3]
data_d$VoteB[data_d$politiciansurvey==4] <- data_d$VoteB_c4[data_d$politiciansurvey==4]

#ttests
ttest_1 <- tidy(t.test(data_d$PoliticianAhealth,data_d$PoliticianBhealth))
ttest_2 <- tidy(t.test(data_d$PoliticianAfuture,data_d$PoliticianBfuture))
ttest_3 <- tidy(t.test(data_d$VoteA,data_d$VoteB))

ttests_d<-bind_rows(ttest_1, ttest_2, ttest_3)
ttests_d$politicianA_mean <- ttests_d$estimate1
ttests_d$politicianB_mean <- ttests_d$estimate2

#labels
topic <- c("Medicare for all", "Future issues", "Vote likelihood")
party <- c("democrat", "democrat", "democrat")
ttests_d$topic <- topic
ttests_d$party <- party
ttests_d <- ttests_d [c("topic", "party", "statistic", "politicianA_mean", "politicianB_mean", "p.value", "conf.low", "conf.high")]

#data wrangling
chart_data <- ttests_d [c("topic", "politicianA_mean", "politicianB_mean")]
ttest_melted <- reshape2::melt(chart_data)
data_tomerge <- ttests_d [c("topic")]
plot_dataD <- merge(ttest_melted, data_tomerge)

#adding 95% confidence intervals
#ideologue
low_D_A_future <- t.test(data_d$PoliticianAfuture)$conf.int[1]
hi_D_A_future <- t.test(data_d$PoliticianAfuture)$conf.int[2]

low_D_A_healthcare <- t.test(data_d$PoliticianAhealth)$conf.int[1]
hi_D_A_healthcare <- t.test(data_d$PoliticianAhealth)$conf.int[2]

low_D_A_vote <- t.test(data_d$VoteA)$conf.int[1]
hi_D_A_vote <- t.test(data_d$VoteA)$conf.int[2]

#moderate
low_D_B_future <- t.test(data_d$PoliticianBfuture)$conf.int[1]
hi_D_B_future <- t.test(data_d$PoliticianBfuture)$conf.int[2]

low_D_B_healthcare <- t.test(data_d$PoliticianBhealth)$conf.int[1]
hi_D_B_healthcare <- t.test(data_d$PoliticianBhealth)$conf.int[2]

low_D_B_vote <- t.test(data_d$VoteB)$conf.int[1]
hi_D_B_vote <- t.test(data_d$VoteB)$conf.int[2]

plot_dataD$ci_low <- c(low_D_A_future, low_D_B_future, low_D_A_healthcare, low_D_B_healthcare, low_D_A_vote, low_D_B_vote)

plot_dataD$ci_hi <- c(hi_D_A_future, hi_D_B_future, hi_D_A_healthcare, hi_D_B_healthcare, hi_D_A_vote, hi_D_B_vote)

#Produce plot
p_d<- ggplot(plot_dataD, aes(x=variable, y=value, fill=topic)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge())+
  scale_x_discrete(limit = c("politicianA_mean", "politicianB_mean"), 
                   labels = c("Ideologue","Moderate"))+
  scale_y_continuous(name="Respondent Inference", limits=c(0, 7), breaks = 1:7)+
  labs(title="Democrat subjects", x="Democrat Politicians", fill="" )+
  geom_errorbar(aes(ymin=ci_low, ymax=ci_hi, width=.2),
                position=position_dodge(.9))+
  theme_classic() + 
  scale_fill_manual(values=c('gray80','gray25', 'gray50'))

p_d <- p_d + theme_classic() + theme(axis.text = element_text(size = 20), axis.title = element_text(size = 22), legend.text = element_text(size = 20), legend.title = element_text(size = 20), title = element_text(size = 22))


#REPUBLICANS####
data_r <- read.csv("study_republicans1.csv")
data_r <- subset(data_r, !(is.na(data_r$gender)))
data_r <- subset(data_r, data_r$partyaff<4) 

#combine outcome variables
data_r$PoliticianAfuture <- data_r$PoliticianAfuture_c1
data_r$PoliticianAfuture[data_r$politiciansurvey==3] <- data_r$PoliticianAfuture_c3[data_r$politiciansurvey==3]
data_r$PoliticianAfuture[data_r$politiciansurvey==4] <- data_r$PoliticianAfuture_c4[data_r$politiciansurvey==4]

data_r$PoliticianBfuture <- data_r$PoliticianBfuture_c1
data_r$PoliticianBfuture[data_r$politiciansurvey==3] <- data_r$PoliticianBfuture_c3[data_r$politiciansurvey==3]
data_r$PoliticianBfuture[data_r$politiciansurvey==4] <- data_r$PoliticianBfuture_c4[data_r$politiciansurvey==4]

data_r$PoliticianAright <- data_r$PoliticianAright_c1
data_r$PoliticianAright[data_r$politiciansurvey==3] <- data_r$PoliticianAright_c3[data_r$politiciansurvey==3]
data_r$PoliticianAright[data_r$politiciansurvey==4] <- data_r$PoliticianAright_c4[data_r$politiciansurvey==4]

data_r$PoliticianBright <- data_r$PoliticianBright_c1
data_r$PoliticianBright[data_r$politiciansurvey==3] <- data_r$PoliticianBright_c3[data_r$politiciansurvey==3]
data_r$PoliticianBright[data_r$politiciansurvey==4] <- data_r$PoliticianBright_c4[data_r$politiciansurvey==4]

data_r$VoteA <- data_r$VoteA_c1
data_r$VoteA[data_r$politiciansurvey==3] <- data_r$VoteA_c3[data_r$politiciansurvey==3]
data_r$VoteA[data_r$politiciansurvey==4] <- data_r$VoteA_c4[data_r$politiciansurvey==4]

data_r$VoteB <- data_r$VoteB_c1
data_r$VoteB[data_r$politiciansurvey==3] <- data_r$VoteB_c3[data_r$politiciansurvey==3]
data_r$VoteB[data_r$politiciansurvey==4] <- data_r$VoteB_c4[data_r$politiciansurvey==4]

#ttests
ttest_1<- tidy(t.test(data_r$PoliticianAright,data_r$PoliticianBright))
ttest_2<- tidy(t.test(data_r$PoliticianAfuture,data_r$PoliticianBfuture))
ttest_3 <- tidy(t.test(data_r$VoteA,data_r$VoteB))

ttests_r<-bind_rows(ttest_1, ttest_2, ttest_3)
ttests_r$politicianA_mean <- ttests_r$estimate1
ttests_r$politicianB_mean <- ttests_r$estimate2

#labels
topic <- c("Right to refuse", "Future issues", "Vote likelihood")
party <- c("republican", "republican", "republican")
ttests_r$topic <- topic
ttests_r$party <- party
ttests_r <- ttests_r [c("topic", "party", "statistic", "politicianA_mean", "politicianB_mean", "p.value", "conf.low", "conf.high")]

#data wrangling
chart_data <- ttests_r [c("topic", "politicianA_mean", "politicianB_mean")]
ttest_melted <-  reshape2::melt(chart_data)
data_tomerge <- ttests_r [c("topic")]
plot_dataR <- merge(ttest_melted, data_tomerge)

#adding 95% confidence intervals
#ideologue
low_R_A_future <- t.test(data_r$PoliticianAfuture)$conf.int[1]
hi_R_A_future <- t.test(data_r$PoliticianAfuture)$conf.int[2]

low_R_A_right <- t.test(data_r$PoliticianAright)$conf.int[1]
hi_R_A_right <- t.test(data_r$PoliticianAright)$conf.int[2]

low_R_A_vote <- t.test(data_r$VoteA)$conf.int[1]
hi_R_A_vote <- t.test(data_r$VoteA)$conf.int[2]

#moderate
low_R_B_future <- t.test(data_r$PoliticianBfuture)$conf.int[1]
hi_R_B_future <- t.test(data_r$PoliticianBfuture)$conf.int[2]

low_R_B_right <- t.test(data_r$PoliticianBright)$conf.int[1]
hi_R_B_right <- t.test(data_r$PoliticianBright)$conf.int[2]

low_R_B_vote <- t.test(data_r$VoteB)$conf.int[1]
hi_R_B_vote <- t.test(data_r$VoteB)$conf.int[2]

plot_dataR$ci_low <- c(low_R_A_future, low_R_B_future, low_R_A_right, low_R_B_right, low_R_A_vote, low_R_B_vote)

plot_dataR$ci_hi <- c(hi_R_A_future, hi_R_B_future, hi_R_A_right, hi_R_B_right, hi_R_A_vote, hi_R_B_vote)

#Produce plot
p_r<- ggplot(plot_dataR, aes(x=variable, y=value, fill=topic)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge())+
  scale_x_discrete(limit = c("politicianA_mean", "politicianB_mean"), 
                   labels = c("Ideologue","Moderate"))+
  scale_y_continuous(name="", limits=c(0, 7), breaks = 1:7)+
  labs(title="Republican subjects", x="Republican Politicians", fill="" )+
  geom_errorbar(aes(ymin=ci_low, ymax=ci_hi, width=.2),
                position=position_dodge(.9))+
  theme_classic() + 
  scale_fill_manual(values=c('gray80','gray25', 'gray50'))
p_r <- p_r + theme_classic() + theme(axis.text = element_text(size = 20), axis.title = element_text(size = 22), legend.text = element_text(size = 20), legend.title = element_text(size = 20), title = element_text(size = 22))

#combined plot
plot_grid(p_d, p_r, labels = NULL)

#######Additional Code######
#To reproduce Figure 4 for "moderate voters" only (see Appendix), limit samples as shown below before t-tests:

data_d <- subset(data_d, data_d$politiciansurvey==3|data_d$politiciansurvey==4)
data_d$police <- ifelse(data_d$politiciansurvey==3, data_d$police_own_c3, data_d$police_own_c4)
data_d <- subset(data_d, data_d$police<4)

data_r <- subset(data_r, data_r$politiciansurvey==3|data_r$politiciansurvey==4)
data_r$guncontrol <- ifelse(data_r$politiciansurvey==3, data_r$guncontrol_own_c3, data_r$guncontrol_own_c4)
data_r <- subset(data_r, data_r$guncontrol>4)

#To reproduce Figure 4 for "ideologue voters" only (see Appendix), limit samples as shown below before t-tests:

data_d <- subset(data_d, data_d$politiciansurvey==3|data_d$politiciansurvey==4)
data_d$police <- ifelse(data_d$politiciansurvey==3, data_d$police_own_c3, data_d$police_own_c4)
data_d <- subset(data_d, data_d$police>4)

data_r <- subset(data_r, data_r$politiciansurvey==3|data_r$politiciansurvey==4)
data_r$guncontrol <- ifelse(data_r$politiciansurvey==3, data_r$guncontrol_own_c3, data_r$guncontrol_own_c4)
data_r <- subset(data_r, data_r$guncontrol<4)


##REPRODUCING TABLE 2#######

##Democrats################

study <- read.csv("study_democrats1.csv")
study <- subset(study, !(is.na(study$gender)))
study <- subset(study, study$partyaff>2 & study$partyaff<6)
nrow(study)

## dividing into four conditions:
## Own opinion only
studyown <- subset(study, study$politiciansurvey==2)

## politician inference only
studyp <- subset(study, study$politiciansurvey==1)

studyp$votedifference <- studyp$VoteA_c1 - studyp$VoteB_c1
studyp$futuredifference <- studyp$PoliticianAfuture_c1 - studyp$PoliticianBfuture_c1
studyp$healthdifference <- studyp$PoliticianAhealth_c1 - studyp$PoliticianBhealth_c1

## leaving only those from combined conditions
## Own opinion and then politician
studyop <- subset(study, study$politiciansurvey==3)

studyop$votedifference <- studyop$VoteA_c3 - studyop$VoteB_c3
studyop$futuredifference <- studyop$PoliticianAfuture_c3 - studyop$PoliticianBfuture_c3
studyop$healthdifference <- studyop$PoliticianAhealth_c3 - studyop$PoliticianBhealth_c3

## Politician and then own opinion
studypo <- subset(study, study$politiciansurvey==4)

studypo$votedifference <- studypo$VoteA_c4 - studypo$VoteB_c4
studypo$futuredifference <- studypo$PoliticianAfuture_c4 - studypo$PoliticianBfuture_c4
studypo$healthdifference <- studypo$PoliticianAhealth_c4 - studypo$PoliticianBhealth_c4

#combine into single dataframe
demcomb <- rbind(studyop, studypo)

#combine other variables
demcomb$guncontrol <- ifelse(demcomb$politiciansurvey==3, demcomb$guncontrol_own_c3, demcomb$guncontrol_own_c4)
demcomb$climatechange <- ifelse(demcomb$politiciansurvey==3, demcomb$climatechange_own_c3, demcomb$climatechange_own_c4)
demcomb$police <- ifelse(demcomb$politiciansurvey==3, demcomb$police_own_c3, demcomb$police_own_c4)
demcomb$medicare <- ifelse(demcomb$politiciansurvey==3, demcomb$medicare_own_c3, demcomb$medicare_own_c4)

#MODEL1
model1 <- lm(votedifference ~ futuredifference, data = studyp)
summary(model1)

#MODEL2
model2 <- lm(votedifference ~ healthdifference, data = studyp)
summary(model2)

#MODEL3
model3 <- lm(votedifference ~ futuredifference + partyaff, data = studyp)
summary(model3)

#MODEL4
model4 <- lm(votedifference ~ healthdifference + partyaff, data = studyp)
summary(model4)

#MODEL5
model5 <- lm(votedifference ~ partyaff+ police +futuredifference+as.factor(politiciansurvey), data=demcomb)
summary(model5)

#MODEL6
model6 <- lm(votedifference ~ partyaff+police+healthdifference+as.factor(politiciansurvey), data=demcomb)
summary(model6)

##Republicans################
study <- read.csv("study_republicans1.csv")
study <- subset(study, !(is.na(study$gender)))
study <- subset(study, study$partyaff<4) #remove the democrat and "other" responses
nrow(study)

## dividing into four conditions:
## Own opinion only
studyown <- subset(study, study$politiciansurvey==2)

## politician inference only
studyp <- subset(study, study$politiciansurvey==1)

studyp$votedifference <- studyp$VoteA_c1 - studyp$VoteB_c1
studyp$futuredifference <- studyp$PoliticianAfuture_c1 - studyp$PoliticianBfuture_c1
studyp$rightdifference <- studyp$PoliticianAright_c1 - studyp$PoliticianBright_c1

## leaving only those from combined conditions
## Own opinion and then politician
studyop <- subset(study, study$politiciansurvey==3)

studyop$votedifference <- studyop$VoteA_c3 - studyop$VoteB_c3
studyop$futuredifference <- studyop$PoliticianAfuture_c3 - studyop$PoliticianBfuture_c3
studyop$rightdifference <- studyop$PoliticianAright_c3 - studyop$PoliticianBright_c3

## Politician and then own opinion
studypo <- subset(study, study$politiciansurvey==4)

studypo$votedifference <- studypo$VoteA_c4 - studypo$VoteB_c4
studypo$futuredifference <- studypo$PoliticianAfuture_c4 - studypo$PoliticianBfuture_c4
studypo$rightdifference <- studypo$PoliticianAright_c4 - studypo$PoliticianBright_c4

#combine into single dataframe
repcomb <- rbind(studyop, studypo)

#combine other variables
repcomb$guncontrol <- ifelse(repcomb$politiciansurvey==3, repcomb$guncontrol_own_c3, repcomb$guncontrol_own_c4)
repcomb$climatechange <- ifelse(repcomb$politiciansurvey==3, repcomb$climatechange_own_c3, repcomb$climatechange_own_c4)
repcomb$police <- ifelse(repcomb$politiciansurvey==3, repcomb$police_own_c3, repcomb$police_own_c4)
repcomb$right <- ifelse(repcomb$politiciansurvey==3, repcomb$righttorefuse_own_c3, repcomb$righttorefuse_own_c4)

#MODEL 7
model7 <- lm(votedifference ~ futuredifference, data=studyp)
summary(model7)

#MODEL 8
model8 <- lm(votedifference ~ rightdifference, data=studyp)
summary(model8)

#MODEL 9
model9 <- lm(votedifference ~ futuredifference + partyaff, data=studyp)
summary(model9)

#MODEL 10
model10 <- lm(votedifference ~ rightdifference + partyaff, data=studyp)
summary(model10)

#MODEL 11
model11 <- lm(votedifference ~ partyaff+guncontrol+futuredifference+as.factor(politiciansurvey), data=repcomb)
summary(model11)

#MODEL 12
model12 <- lm(votedifference ~ partyaff+guncontrol++rightdifference+as.factor(politiciansurvey), data=repcomb)
summary(model12)





